﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace HRDexCapstoneProject.Functions.Forms
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True");

         string SessionID()
        {
           return Session["EmployeeID"].ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Select();

            SqlCommand cmd = new SqlCommand("SELECT EmployeeID, LastName + ', ' + FirstName + ' ' + MiddleName AS Name, DeptID, catPositionsID, CollegeID FROM formEmployees", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("dt");

            da.Fill(dt);

            txtEmpNo.Text = dt.Rows[0].ItemArray[0].ToString();
            txtEmpName.Text = dt.Rows[0].ItemArray[1].ToString();
            ddlDept.SelectedValue = dt.Rows[0].ItemArray[2].ToString();
            ddlPosition.SelectedValue = dt.Rows[0].ItemArray[3].ToString();
            ddlCol.SelectedValue = dt.Rows[0].ItemArray[4].ToString();
            txtEmpName.Enabled = false;
            txtEmpNo.Enabled = false;
            ddlDept.Enabled = false;
            ddlPosition.Enabled = false;
            ddlCol.Enabled = false;

            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string LeaveID = Request.QueryString["LeaveID"];
            if (txtLeaveID.Text != LeaveID)
            {
                
           
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();

            conn.Open();

            SqlCommand cmd = new SqlCommand("Insert Into formLeave (EmployeeID, PositionID, DepartmentID, CollegeID, CatLeaveID, fromDateTime, toDateTime, Reason, LeaveAddress, totalDaysLeave) " +
                "Values (@EmpID, @posID, @deptID, @colID,@catLeaveID,@fdatetime, @tdatetime, @reason, @leaveAdd, @totalDaysLeave )", conn);
            
            cmd.Parameters.Add("@EmpID", SqlDbType.VarChar).Value = txtEmpNo.Text;
            cmd.Parameters.Add("@posID", SqlDbType.VarChar).Value = ddlPosition.SelectedValue;
            cmd.Parameters.Add("@deptID", SqlDbType.VarChar).Value = ddlDept.SelectedValue;
            cmd.Parameters.Add("@colID", SqlDbType.VarChar).Value = ddlCol.SelectedValue;
            cmd.Parameters.Add("@catLeaveID", SqlDbType.VarChar).Value = ddlLeave.Text;
            cmd.Parameters.Add("@fdatetime", SqlDbType.VarChar).Value = dpFrom.SelectedDateString + " - " + ddl0Hour.Text + ":" + ddl0Min.Text;
            cmd.Parameters.Add("@tdatetime", SqlDbType.VarChar).Value = dpTo.SelectedDateString + " - " + ddl1Hour.Text + ":" + ddl1Min.Text;
            cmd.Parameters.Add("@reason", SqlDbType.VarChar).Value = txtReason.Text;
            cmd.Parameters.Add("@leaveAdd", SqlDbType.VarChar).Value = txtAddress.Text;

            cmd.Parameters.Add("@totalDaysLeave", SqlDbType.VarChar).Value = txtTotalDays.Text;
            
            SqlCommand cmdSelect = new SqlCommand("", conn);
            SqlDataAdapter daS = new SqlDataAdapter(cmdSelect);
            DataTable dtS = new DataTable("dtS");

            cmd.ExecuteNonQuery();

            conn.Close();
            }
            else
            {
                Edit();
            }
        }

        public void Select()
        {
            string LeaveID = Request.QueryString["LeaveID"];
            
            if (LeaveID != null)
            {
                txtAddress.Enabled = false;
                txtEmpName.Enabled = false;
                txtEmpNo.Enabled = false;
                txtFrom.Enabled = false;
                txtLeaveID.Enabled = false;
                txtReason.Enabled = false;
                txtTo.Enabled = false;
                txtTotalDays.Enabled = false;
                ddlCol.Enabled = false;
                ddlDept.Enabled = false;
                ddlLeave.Enabled = false;
                ddlPosition.Enabled = false;

                SqlCommand cmd2 = new SqlCommand("SELECT formLeave.LeaveID, formLeave.CatLeaveID, formLeave.fromDateTime, formLeave.toDateTime, formLeave.totalDaysLeave, formLeave.Reason, formLeave.LeaveAddress, formLeave.EmployeeID, formEmployees.LastName + ', ' + formEmployees.FirstName + ' ' + formEmployees.MiddleName AS Name, formLeave.DepartmentID, formLeave.PositionID, formLeave.CollegeID, formLeave.ApprovalIDHead, formLeave.ApprovalIDDean, formLeave.ApprovalIDHR FROM formLeave INNER JOIN formEmployees ON formLeave.EmployeeID = formEmployees.EmployeeID" +
                    " WHERE (formLeave.LeaveID=" + LeaveID + ")", conn);
                SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
                DataTable dt2 = new DataTable("dt2");

                da2.Fill(dt2);
                txtLeaveID.Text = dt2.Rows[0].ItemArray[0].ToString();
                ddlLeave.SelectedValue = dt2.Rows[0].ItemArray[1].ToString();
                txtFrom.Text = dt2.Rows[0].ItemArray[2].ToString();
                txtTo.Text = dt2.Rows[0].ItemArray[3].ToString();
                txtTotalDays.Text = dt2.Rows[0].ItemArray[4].ToString();
                txtReason.Text = dt2.Rows[0].ItemArray[5].ToString();
                txtAddress.Text = dt2.Rows[0].ItemArray[6].ToString();
                txtEmpNo.Text = dt2.Rows[0].ItemArray[7].ToString();
                txtEmpName.Text = dt2.Rows[0].ItemArray[8].ToString();
                ddlDept.SelectedValue = dt2.Rows[0].ItemArray[9].ToString();
                ddlPosition.SelectedValue = dt2.Rows[0].ItemArray[10].ToString();
                ddlCol.SelectedValue = dt2.Rows[0].ItemArray[11].ToString();



                SqlCommand cmd3 = new SqlCommand("SELECT EmployeeID, UserLevelID, catPositionsID, DeptID, CollegeID FROM formEmployees WHERE (EmployeeID = '" + SessionID() + "')", conn);
                SqlDataAdapter da3 = new SqlDataAdapter(cmd3);
                DataTable dt3 = new DataTable("dt3");
                da3.Fill(dt3);
                //notYetTested
                SqlCommand cmd4 = new SqlCommand("SELECT EmployeeID, UserLevelID, catPositionsID, DeptID, CollegeID FROM formEmployees WHERE (DeptID = "+dt3.Rows[0].ItemArray[3].ToString()+")", conn);
                SqlDataAdapter da4 = new SqlDataAdapter(cmd4);
                DataTable dt4 = new DataTable("dt4");
                da3.Fill(dt4);
                //okey
                if (dt3.Rows[0].ItemArray[4].ToString() == "8" && dt3.Rows[0].ItemArray[3].ToString() == "9" && dt3.Rows[0].ItemArray[1].ToString() == "1" && dt3.Rows[0].ItemArray[2].ToString() == "9")
                {
                    ddlApproval1.Enabled = false;
                    ddlApproval2.Enabled = false;
                    ddlApproval3.Enabled = true;
                }
                //Okey
                else if (dt3.Rows[0].ItemArray[4].ToString() == "8" && dt3.Rows[0].ItemArray[3].ToString() == "10" && dt3.Rows[0].ItemArray[1].ToString() == "2" && dt3.Rows[0].ItemArray[2].ToString() == "4")
                {
                    ddlApproval1.Enabled = false;
                    ddlApproval2.Enabled = false;
                    ddlApproval3.Enabled = true;
                }
                //else if (dt3.Rows[0].ItemArray[4].ToString() == ddlCol.SelectedValue && dt3.Rows[0].ItemArray[3].ToString() == ddlDept.SelectedValue && dt3.Rows[0].ItemArray[2].ToString() == "Employee" || dt3.Rows[0].ItemArray[2].ToString() == "Faculty")
                //{
                //    if (dt3.Rows[0].ItemArray[3].ToString() == "Dean" || dt3.Rows[0].ItemArray[3].ToString() == "Director")
                //    {
                //        ddlApproval1.Enabled = false;
                //        ddlApproval2.Visible = true;
                //        ddlApproval3.Enabled = false;
                //    }
                //}
                //    else if (dt3.Rows[0].ItemArray[3].ToString() == "Supervisor" || dt3.Rows[0].ItemArray[3].ToString() == "Head")
                //    {
                //        ddlApproval1.Visible = true;
                //        ddlApproval2.Enabled = false;
                //        ddlApproval3.Enabled = false;
                //    }
                //    else
                //    {
                //        ddlApproval2.Visible = false;
                //        ddlApproval3.Visible = false;
                //        ddlApproval1.Visible = false;
                //    }



                //Approval Enable Dept Head
                if (dt2.Rows[0].ItemArray[12].ToString() != "0" && dt2.Rows[0].ItemArray[12].ToString() != "1")
                {
                    ddlApproval2.Enabled = false;
                    ddlApproval1.Enabled = true;
                    ddlApproval3.Enabled = false;
                }
                else if (dt2.Rows[0].ItemArray[13].ToString() != "0" && dt2.Rows[0].ItemArray[13].ToString() != "1")
                {
                    ddlApproval2.Enabled = true;
                    ddlApproval1.Enabled = false;
                    ddlApproval3.Enabled = false;
                }
                else if (dt2.Rows[0].ItemArray[14].ToString() != "0" && dt2.Rows[0].ItemArray[14].ToString() != "1")
                {
                    ddlApproval2.Enabled = false;
                    ddlApproval1.Enabled = false;
                    ddlApproval3.Enabled = true;
                }
                else
                {
                    ddlApproval2.Enabled = false;
                    ddlApproval1.Enabled = false;
                    ddlApproval3.Enabled = false;
                }

                

            }
        }
        public void Edit()
        {
            string LeaveID = Request.QueryString["LeaveID"];
            SqlCommand cmd = new SqlCommand("UPDATE formLeave SET  ApprovalIDHead =" + ddlApproval1.Text + " WHERE(LeaveID = " + LeaveID + ")",conn);
            SqlCommand cmd22 = new SqlCommand("UPDATE formLeave SET  ApprovalIDDean =" + ddlApproval2.Text + " WHERE(LeaveID = " + LeaveID + ")",conn);
            SqlCommand cmd33 = new SqlCommand("UPDATE formLeave SET  ApprovalIDHR =" + ddlApproval3.Text + " WHERE(LeaveID = " + LeaveID + ")",conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            SqlDataAdapter da22 = new SqlDataAdapter(cmd22);
            SqlDataAdapter da33 = new SqlDataAdapter(cmd33);
            conn.Open();
            cmd.Parameters.AddWithValue("ApprovalIDHead", ddlApproval1.Text);
            cmd22.Parameters.AddWithValue("ApprovalIDDean", ddlApproval2.Text);
            cmd33.Parameters.AddWithValue("ApprovalIDHR", ddlApproval3.Text);
            cmd.ExecuteNonQuery();
            conn.Close();
        
        }
    }
}